package algocasts;

public class P127_FindMinimumInRotatedSortedArray {
    // Time: O(log(n))  Space: O(1)
    public int findMin(int[] nums) {
        int low = 0, high = nums.length - 1;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] > nums[high]) low = mid + 1;
            else high = mid;
        }
        return nums[low];
    }

    // Time: O(log(n))  Space: O(1)
    public int findMin_EarlyReturn(int[] nums) {
        int low = 0, high = nums.length - 1;
        if (nums[low] < nums[high]) return nums[low];
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] > nums[high]) low = mid + 1;
            else high = mid;
        }
        return nums[low];
    }
}
